package com.sp2.dev.other.xml;


import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: zhimingli
 * Date: 14-3-3
 * Time: 下午12:13
 * To change this template use File | Settings | File Templates.
 */
public class Dom4jTest {
    public static void main(String[] args) {
        List<SiteMapVO> listSiteMap = new ArrayList<SiteMapVO>();
        for (int i = 0; i < 10; i++) {
            SiteMapVO vo = new SiteMapVO();
            vo.setId(i);
            vo.setLoc("loc" + i);
            vo.setPriority("priority" + i);
            vo.setType("type");
            listSiteMap.add(vo);
        }


        Document document = DocumentHelper.createDocument();  //创建文档
        Element employees = document.addElement("urlset");
        employees.setAttributeValue("xmlns","http://www.sitemaps.org/schemas/sitemap/0.9");
//        for (int i = 0; i < listSiteMap.size(); i++) {
//            SiteMapVO vo = listSiteMap.get(i);
//            Element employee = employees.addElement("url");
//            Element loc = employee.addElement("loc");   //添加子节点
//            Element priority = employee.addElement("priority");   //添加子节点
//            loc.setText(vo.getLoc());
//            priority.setText(vo.getPriority());
//            employees.add(employee);
//        }


        Element employee = employees.addElement("employee");
        Element name = employee.addElement("name");   //添加子节点
        name.setText("ddvip"); //添加Text值；例：<a>abc</a>
        name.setAttributeValue("sa", "sa"); //添加属性；例：<a item="item"></a>
        Element sex = employee.addElement("sex");
        sex.setText("m");
        Element age = employee.addElement("age");
        age.setText("29");

        String fileName = "D:\\opt\\wikihtml\\sitemap\\user.xml";
        try {
            Writer fileWriter = new FileWriter(fileName);
            XMLWriter xmlWriter = new XMLWriter(fileWriter);
            xmlWriter.write(document);   //写入文件中
            xmlWriter.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
